import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class Test15 {
	public static void main(String[] args) {

		Test15 a = new Test15();
		a.timeZone2();

	}
	
	private void dd(){
	    Calendar c = Calendar.getInstance();
	    System.out.println("current: "+c.getTime());

	    TimeZone z = c.getTimeZone();
	    int offset = z.getRawOffset();
	    if(z.inDaylightTime(new Date())){
	        offset = offset + z.getDSTSavings();
	    }
	    int offsetHrs = offset / 1000 / 60 / 60;
	    int offsetMins = offset / 1000 / 60 % 60;

	    System.out.println("offset: " + offsetHrs);
	    System.out.println("offset: " + offsetMins);

	    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
	    c.add(Calendar.MINUTE, (-offsetMins));

	    System.out.println("GMT Time: "+c.getTime());
	    System.out.println("GMT Time: "+c.get(Calendar.HOUR_OF_DAY));
	    
	}
	
	private void display(){
	       // timezone independent date (usually interpreted by the timezone of the default locale of the user machine)
	    Date now = new Date();

	    // now lets get explicit with how we wish to interpret the date
	    Calendar london =  Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
	    Calendar paris = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"));

	    (london.get(Calendar.MONTH)+1)+"-"+london.get(Calendar.DAY_OF_MONTH);
	    // now set the same date on two different calendar instance
	    london.setTime(now);
	    paris.setTime(now);

	    // the time is the same
	    assert london.getTimeInMillis() == paris.getTimeInMillis();

	    // London is interpreted one hour earlier than Paris (as of post date of 9th May 2012)
	    String londonTime = london.get(Calendar.HOUR) + ":" + london.get(Calendar.MINUTE);
	    String londonTZ = london.getTimeZone().getDisplayName(london.getTimeZone().inDaylightTime(london.getTime()), TimeZone.SHORT);
	    System.out.println(londonTime + " " + londonTZ);

	    // Paris is interpreted one hour later than Paris (as of post date of 9th May 2012)
	    String parisTime = paris.get(Calendar.HOUR) + ":" + paris.get(Calendar.MINUTE);
	    String parisTZ = paris.getTimeZone().getDisplayName(paris.getTimeZone().inDaylightTime(paris.getTime()), TimeZone.SHORT);
	    System.out.println(parisTime + " " + parisTZ);
	}
	
	private void test(){
		Date now = new Date();
		Calendar t =Calendar.getInstance();
		t.setTime(now);
		System.out.println(t.getTime());
		t.add(Calendar.HOUR_OF_DAY, -1);
		System.out.println(t.getTime());
		
	}

	private void timeZone() {
//		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
		Calendar cal1 = Calendar.getInstance();
		cal1.set(2001, 7, 1, 0, 0, 0);
		System.out.println(cal1.getTime());
		// 在当前的时区下，我们指定了时间是 8 月，显示 7
		System.out.println(cal1.get(Calendar.MONTH));
		// 在当前的时区下，我们指定了时间是 1 日，显示 1
		System.out.println(cal1.get(Calendar.DATE));
		// 在当前的时区下，我们指定了时间是 0 时，显示 0
		System.out.println(cal1.get(Calendar.HOUR_OF_DAY));
		System.out.println(cal1.getTimeZone().getID());
		cal1.setTimeZone(TimeZone.getTimeZone("GMT"));
//		cal1.set(Calendar.MILLISECOND, 0);
		System.out.println(cal1.getTime());
		System.out.println(cal1.getTimeZone().getID());
		// 由于实际保存的时间没有修改，在 GMT 下，由于时差，显示 6 （7月）
		System.out.println(cal1.get(Calendar.MONTH));
		// 在 GMT 下，由于时差，显示 31
		System.out.println(cal1.get(Calendar.DATE));
		// 由于更改了时区，在 GMT 下时间就会变成 16，8个小时时差
		System.out.println(cal1.get(Calendar.HOUR_OF_DAY));
	}
	private void timeZone2() {
//		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
		Date now = new Date();
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		
		cal1.setTime(now);
		cal2.setTime(now);
		
		System.out.println(cal1.getTime());
		System.out.println(cal2.getTime());
		
		cal1.setTimeZone(TimeZone.getTimeZone("GMT"));
		System.out.println(cal1.getTime());
		
		
	}
	
	
}